package com.ingrid.babySitter.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

public class DAO {

	private Connection connection;

	public DAO() throws ClassNotFoundException, SQLException {
		Class.forName("org.postgresql.Driver");

		this.connection = DriverManager.getConnection(
				"jdbc:postgresql://127.0.0.1:5432/baby_sitter_desktop",
				"postgres", "postgres");
	}

	public void inserirBaba(Baba baba) throws SQLException {
		PreparedStatement statement = this.connection
				.prepareStatement("INSERT INTO babas (nome, endereco, telefone) VALUES (?,?,?)");
		statement.setString(1, baba.getNome());
		statement.setString(2, baba.getEndereco());
		statement.setLong(3, baba.getTelefone());
		statement.execute();

		long idBaba = 1;

		ArrayList<Dia> dias = baba.getDias();
		for (int i = 0; i < dias.size(); i++) {
			Dia d = dias.get(i);
			ArrayList<Horario> horarios = d.getHorarios();
			for (int j = 0; j < horarios.size(); j++) {
				Horario h = horarios.get(j);
				inserirHorario(idBaba, d, h);
			}
		}
	}

	private void inserirHorario(long idBaba, Dia dia, Horario horario)
			throws SQLException {
		PreparedStatement statement = this.connection
				.prepareStatement("INSERT INTO horarios (baba_id, dia, hora_inicio, minuto_inicio, hora_fim, minuto_fim) VALUES (?,?,?,?,?,?)");
		statement.setLong(1, idBaba);
		statement.setLong(2, dia.getDia());
		statement.setLong(3, horario.getHoraInicio());
		statement.setLong(4, horario.getMinutoInicio());
		statement.setLong(5, horario.getHoraFim());
		statement.setLong(6, horario.getMinutoFim());
		statement.execute();
	}

	public void inserirCliente(Cliente cliente) throws SQLException {
		PreparedStatement statement = this.connection.prepareStatement("INSERT INTO clientes(nome, telefone, endereco) VALUES(?,?,?)");
		statement.setString(1, cliente.getNome());
		statement.setLong(2, cliente.getTelefone());
		statement.setString(3, cliente.getEndereco());
		statement.execute();
	}
}
